package cn.itsource.rpms.web.controller;

import cn.itsource.rpms.domain.RepairOrder;
import cn.itsource.rpms.query.RepairOrderQuery;
import cn.itsource.rpms.service.IRepairOrderItemService;
import cn.itsource.rpms.service.IRepairOrderService;
import cn.itsource.rpms.util.AjaxResult;
import cn.itsource.rpms.util.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@RequestMapping("/repairorder")
@CrossOrigin
public class RepairOrderController {
    @Autowired
    private IRepairOrderService service;
    @Autowired
    private IRepairOrderItemService itemService;
    @RequestMapping(value = "/queryAll",method = RequestMethod.PATCH)
    @ResponseBody
    public List<RepairOrder> queryAll(){
        return service.queryAll();
    }
    @RequestMapping(value = "/queryData",method = RequestMethod.PATCH)//条件查询
    @ResponseBody
    public PageResult<RepairOrder> queryData(@RequestBody RepairOrderQuery query){//requestbody接收前端传递的json字符串形式的数据
        PageResult pageResult = service.queryPage(query);
        return pageResult;
    }
    @RequestMapping(value = "/{id}",method = RequestMethod.GET)
    @ResponseBody
    public RepairOrder queryOne(@PathVariable Long id){//将地址栏的id作为参数来接收
        System.out.println(id);
        return service.queryOne(id);
    }
    @RequestMapping(value = "/add",method = RequestMethod.PUT)
    @ResponseBody
    public AjaxResult add(@RequestBody RepairOrder repairOrder){//将接收过来的json数据转换成对应的实体类对象
        try {
            service.add(repairOrder);
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult("保存失败");//保存失败就返回不保存失败的ajaxresult
        }
        return new AjaxResult();//成功就使用默认的
    }
    @RequestMapping(value = "/{id}",method = RequestMethod.DELETE)
    @ResponseBody
    public AjaxResult delete(@PathVariable Long id){
        try {
            RepairOrder repairOrder = service.queryOne(id);
            Integer opt_id = repairOrder.getOpt_id();
            itemService.delete(opt_id);
            service.delete(id);
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult("删除失败");
        }
        return new AjaxResult();
    }
    @RequestMapping(value = "update",method = RequestMethod.POST)
    @ResponseBody
    public AjaxResult update(@RequestBody RepairOrder repairOrder){
        try {
            service.update(repairOrder);
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult("修改失败");
        }
        return new AjaxResult();
    }
}
